#include "Timer.h"

#include "Switch.h"

/* CONSTRUCTOR */
Switch::Switch(SwitchListener* switchListener, int switchPort)
{
  Timer* t = Timer::getTimer();
  m_switchPort = switchPort;
  m_switchListener = switchListener;
  pinMode(m_switchPort, INPUT);
  
  t->every( 100, this);
}

/* METHODES */

/**
* \brief Verify if switch position has changed and update its state
* \param eventId
* \return none
*/
void Switch::timerCallbackFunction(int eventId)
{
  boolean oldState = m_state;
  m_state = digitalRead(m_switchPort);
  if (m_state == oldState)
  {
    /* state hasn't change, nothing to do */
  }
  else
  {
    m_switchListener->switchPositionChanged(m_state);
  }
}


